package com.lanou.activity.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.activity.Service.SaleCdnService;
import com.lanou.activity.utils.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/saleCdn")
public class SaleCdnController {
    private static final Logger logger = LoggerFactory.getLogger(SaleCdnController.class);
    @Resource
    private SaleCdnService saleCdnService;


    /**
     * @param
     * @return
     * @description 跳转到 促销条件添加页面 addSaleCdn.ftl
     * @author Lonely
     * @date 17/9/23
     */
    @RequestMapping("findSaleCdn")
    public ModelAndView findSaleCdn() {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> list = saleCdnService.findSupplier();
        modelAndView.addObject("list", list);
        modelAndView.setViewName("addSaleCdn");

        return modelAndView;
    }

    /**
     * @param
     * @return
     * @description 添加 促销条件表的数据
     * @author Lonely
     * @date 17/9/23
     */
    @RequestMapping("addSaleCdnInfo")
    @ResponseBody
    public Map<String, Object> addSaleCdnInfo(@RequestParam Map<String, Object> params) {

//        使用账号生成器自动生成促销账号
        String conNo = ToolUtil.AccountGenerator("con");
        params.put("conNo", conNo);

        saleCdnService.addSaleCdnInfo(params);
        params.put("result", "success");
        return params;
    }


    /**
     * @param
     * @return
     * @description 跳转到 促销条件管理页面 saleCndfManage.ftl
     * @author
     * @date 17/9/23
     */
    @RequestMapping("saleCndfManage")
    public ModelAndView saleCndfManage(@RequestParam Map<String, Object> params) {
        logger.info(params.toString());
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> list = saleCdnService.findSupplier();
        modelAndView.addObject("list", list);
        modelAndView.addObject("params", params);
        PageInfo<Map<String, Object>> pageInfo =  saleCdnService.findSaleCdnList(params);
        modelAndView.addObject("page", pageInfo);
        modelAndView.addObject("list1", pageInfo.getList());
        modelAndView.setViewName("saleCndfManage");
        return modelAndView;
    }

    /**
     * @param
     * @return
     * @description 跳转到 促销条件管理页面 saleCndfbyconNo.ftl con_no
     * @author
     * @date 17/9/23
     */
    @RequestMapping("saleCndfbyconNo")
    public ModelAndView saleCndfbyconNo(@RequestParam Map<String, Object> params) {
        logger.info(params.toString());
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> list = saleCdnService.findSupplier();
        PageInfo<Map<String, Object>> pageInfo =  saleCdnService.findSaleCdnList(params);
        Map<String, Object> map =pageInfo.getList().get(0);
        logger.info(map.toString());
        modelAndView.addObject("param", map);
        modelAndView.addObject("list", list);
        modelAndView.setViewName("saleCndbyconNo");
        return modelAndView;
    }

    /**
     * @param
     * @return
     * @description 添加 促销条件表的数据
     * @author Lonely
     * @date 17/9/23
     */
    @RequestMapping("updataSaleCdnInfo")
    @ResponseBody
    public Map<String, Object> updataSaleCdnInfo(@RequestParam Map<String, Object> params) {
        logger.info(params.toString());
//        params.put("mdf_date",ToolUtil.dataTool());
        saleCdnService.updateSaleCdn(params);
        params.put("result", "success");
        return params;
    }
}
